package com.etool.action.curd;


import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;

import com.etool.modules.curd.model.Column;
import com.etool.modules.curd.service.CurdService;
import com.etool.modules.curd.util.CurdConfigUtil;
import com.opensymphony.xwork2.ActionSupport;
public class UpdateAction extends ActionSupport{
	
	private HttpServletResponse response=ServletActionContext.getResponse();
	private HttpServletRequest request=ServletActionContext.getRequest();
	private String target;

	private CurdService curdService;

	
	

	@Action(value="update")
	public String create() throws IOException
	{
		CurdConfigUtil.setSecondJdbc(curdService, target);
		List<Column> cols=curdService.desc(target);
		
		Map<String, Object> params=new HashMap<String, Object>();
		
		for(Column item:cols)
		{
			params.put(item.getName(), request.getParameter(item.getName()));
		}
		
		curdService.update(target, params);
		CurdConfigUtil.disableSecondJdbc(curdService);
		response.setContentType("text/html;charset=utf-8");
		response.getWriter().write(JSONObject.fromObject(Collections.singletonMap("success", true)).toString());
		response.getWriter().flush();
		
		return NONE;
	}
	
	public String getTarget() {
		return target;
	}

	public void setTarget(String target) {
		this.target = target;
	}
	

	
	public CurdService getCurdService() {
		return curdService;
	}

	public void setCurdService(CurdService curdService) {
		this.curdService = curdService;
	}



}
